import { _decorator, Color, Component, find, Graphics, Intersection2D, math, Node, Rect, Sprite, tween, UITransform, v3, view } from 'cc';
import { AudioConfig, GameConst } from './config/game-param';
import { EventManager } from './manager/event';
import { AudioMgr } from './AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('Pipe')
export class Pipe extends Component {
    @property({
        type: Sprite,
        displayName: '上方管道-pipeUp'
    })
    private pipeUp: Sprite

    @property({
        type: Sprite,
        displayName: '下方管道-pipeDown'
    })
    private pipeDown: Sprite

    @property isThrough: boolean = false

    private bird: Node = null

    private speed: number = GameConst.MOVE_SPEED

    private _initPosX: number

    private gapW: number = GameConst.PIPE_WIDTH
    private gapH: number = 0
  
    onLoad() {
        this._initPosX = view.getVisibleSize().width / 2 + 100
        this.initPos()

        this.bird = find('Canvas/Bird/bird')
    }

    update(deltaTime: number) {
        this.pipeMove(deltaTime)
        this.checkThrough()
    }

    initPos() {
        const gap = math.randomRangeInt(100, 200)
        const range = math.randomRangeInt(-200, 200)

        this.pipeUp.node.setPosition(0, gap)
        this.pipeDown.node.setPosition(0, -gap)
        this.node.setPosition(this._initPosX, range)

        this.gapH = gap * 2
    }

    pipeMove(deltaTime: number) {
        const distance = this.speed * deltaTime
        const { x, y } = this.node.position
        this.node.setPosition(x - distance, y)

        if(this.node.position.x < -1200) this.node.destroy()
    }

    moveBack(index: number) {
        tween(this.node)
            .to(0.5, { position: v3(this._initPosX + GameConst.MOVE_SPEED * index , 0, 0) }, {
                onComplete: () => {
                    if (this.node) this.node.destroy()
                }
            }).start()
    }

    checkThrough() {
        if (this.isThrough) return

        const pos = this.node.getComponent(UITransform).convertToNodeSpaceAR(this.bird.worldPosition)

        // 绘制两个矩形，矩形gap和矩形小鸟
        const rectGap = new Rect(this.pipeDown.node.position.x - GameConst.PIPE_WIDTH / 2, this.pipeDown.node.position.y, this.gapW, this.gapH)
        const rectBird = new Rect(pos.x - GameConst.BIRD_WIDTH / 2, pos.y - GameConst.BIRD_HEIGHT / 2, GameConst.BIRD_WIDTH, GameConst.BIRD_HEIGHT)
        
        const checked = Intersection2D.rectRect(rectGap, rectBird)

        if (checked) {
            this.isThrough = true
            EventManager.emit('add-a-point')
            AudioMgr.inst.playOneShot(AudioConfig.POINT)
        }

        // 这里碰撞测试代码
        // const rectBirdNode = this.drawRect('bird-rect', rectBird.x, rectBird.y, rectBird.width, rectBird.height)
        // const rectGapNode = this.drawRect('gap-rect', rectGap.x, rectGap.y, rectGap.width, rectGap.height)
        // this.node.addChild(rectBirdNode)
        // this.node.addChild(rectGapNode)
        // console.log(checked)
    }

    drawRect(name: string, x: number, y: number, width: number, height: number): Node {
        const node = new Node(name)
        let graphicsGap = node.addComponent(Graphics)
        graphicsGap.lineWidth = 2
        graphicsGap.rect(x, y, width, height)
        graphicsGap.fillColor = new Color().fromHEX('#0000ff')
        graphicsGap.fill()
        // this.node.addChild(gapNode)
        return node
    }
}


